package com.ruoyi.web.controller.common;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.code.kaptcha.Producer;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.util.SendMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 手机验证码操作处理
 *
 * @author ruoyi
 */
@RestController
public class PhoneController {
    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysUserService userService;


    /**
     * 生成手机验证码
     */
    @GetMapping("/phoneVerification")
    public AjaxResult phoneVerification(SysUser user) throws IOException {
        AjaxResult ajax = AjaxResult.success();
        boolean captchaOnOff = configService.selectCaptchaOnOff();
        ajax.put("captchaOnOff", captchaOnOff);
        if (!captchaOnOff) {
            return ajax;
        }
        // 保存验证码信息
        String code = null;
        code = randomCode();
        // 一分钟内不允许再次请求验证码
        redisCache.setCacheObject(user.getUserName(), code, 1, TimeUnit.MINUTES);
        SendMsg.getData("phone="+user.getUserName()+"&content=【松原石化园区】您的验证码是"+code+"，请妥善保管&enc=99015");
        // 转换流信息写出
//        HttpUtil.get("https://sctapi.ftqq.com/SCT144649Tj21ar2tHI1Nm8KXMqc9Q0mbI.send?title=验证码&desp="+code+"");
        System.out.println(code);
        ajax.put("phone", user.getUserName());
        return ajax;
    }

    @PostMapping("phoneBack")
    public AjaxResult phoneBack(SysUser user){
        String captcha = redisCache.getCacheObject(user.getUserName());
        if(user.getCode().equals(captcha)){
            return AjaxResult.success(true);
        }else{
            return AjaxResult.error("短信验证码错误");
        }
    }


    /**
     * 找回密码
     * @param user
     * @return
     */
    @PostMapping("retrievePassword")
    public AjaxResult retrievePassword(SysUser user){
        SysUser user1 = userService.selectUserByUserName(user.getUserName());
        user1.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        userService.updateUser(user1);
        return AjaxResult.success(true);
    }

    @PostMapping("retrievePassword1")
    public AjaxResult retrievePassword1(SysUser user){
        SysUser user1 = userService.selectUserByUserName(user.getUserName());
        user1.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        userService.updateUser(user1);
        return AjaxResult.success(true);
    }

    /**
     * 建议使用
     */
    public static String randomCode() {
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }
}
